main_dish = []
snacks = []
seafood = []
salad = []
soups = []
wheaten_food = []
breakfast = []
condiments = []
dessert = []
drink = []
appetizer = []
side_dish = []

import os, re
def is_number_dot_start(s):
    if '"' in s: return False
    return bool(re.match(r'\d+\.', s))

# 去掉无图片内容
nopic = set([])
for line in os.popen("ls recipes/*.md").read().split("\n"):
    if "/" not in line: continue
    name = line.split("/")[1].split(".")[0]
    nopic.add(name.strip())
for line in os.popen("ls static/.").read().split("\n"):
    if "." in line or line.isdigit() == False: continue
    if line in nopic: nopic.remove(line.strip())

lists = [10564,11368,12100,14603,148,18798,18876,19556,19585,19666,1966,19671,19693,19695,19939,21140,22711,22720,22741,24041,24568,24647,24717,24801,25663,25976,2710,30848,35748,37087,39173,46212,47961,47974,48225,48272,4841,49373,50962,50963,50964,50965,51020,51109,51119,51151,51347,51534,51538,53289,53456,53518,54818,55556,55558,55563,55564,55572,55575,55578,55942,55945,55947,55960,55965,55969,56225,5626,56310,56312,5632,56335,56343,56346,56348,56361,57374,57442,57754,57771,58218,58429,58453,58827,60215,60818,61063,61606,61608,61666,61684,62650,63687,64272,65458,66675,8124]
for line in open("tmp.txt","r"):
    id,name,tag,topic = line.strip().split(",")
    name = name.replace('&#39;',"'")
    if int(id) in lists: continue 
    if str(id) in nopic: continue
    sentence = list(filter(lambda x: is_number_dot_start(x) == False and len(x) > 12 and ":" not in x and len(x) < 210 and 'Enjoy' not in x and '**' not in x, open("./recipes/%s.md"%id,'r').read().replace('\u200b','\n').replace('“','"').replace('”','"').replace("    "," ").split("\n")))
    length = [ (i,len(sent)) for i,sent in enumerate(sentence) ]
    print(id)
    try:
        desc  = sentence[sorted(length,key=lambda x: x[1])[-1][0]].replace("\t"," ")
    except:
        os.system("sudo python3 run.py %s"%id)
    if bool(re.match(r'\d+\.', desc)): desc = ".".join(desc.split(".")[1:])
    if desc is None: exit()
    for dot in ["·","**","*","-"]:
       if desc.strip().startswith(dot): desc = desc.strip().replace(dot,"").strip()
    topics = set([ wd.lower().strip().replace("_"," ") for wd in topic.split("|")])
    if len(topics & set(['salads','salad','vegetables'])) > 0: salad.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['main dish','main dishes'])) > 0: main_dish.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['snacks','snack'])) > 0: snacks.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['seafood','sea food'])) > 0: seafood.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['soups','soup'])) > 0: soups.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['cookies','breakfast','sandwiches','bread','hamburg'])) > 0: breakfast.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['condiments','salsa','dips','sauces','dressings','seasonings'])) > 0: condiments.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['cake','wheaten food','wheat food','pasta','pizza'])) > 0: wheaten_food.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['dessert','baking','desserts'])) > 0: dessert.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['drink','drinks'])) > 0: drink.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['appetizer','appetizers'])) > 0: appetizer.append(id+"\t"+name+"\t"+desc)
    if len(topics & set(['side dish','side dishes'])) > 0: side_dish.append(id+"\t"+name+"\t"+desc)
with open("./listpack/salad.txt","w") as f: f.write("\n".join(salad))
with open("./listpack/main_dishes.txt","w") as f: f.write("\n".join(main_dish))
with open("./listpack/snacks.txt","w") as f: f.write("\n".join(snacks))
with open("./listpack/soup.txt","w") as f: f.write("\n".join(soups))
with open("./listpack/wheaten_food.txt","w") as f: f.write("\n".join(wheaten_food))
with open("./listpack/breakfast.txt","w") as f: f.write("\n".join(breakfast))
with open("./listpack/condiments.txt","w") as f: f.write("\n".join(condiments))
with open("./listpack/dessert.txt","w") as f: f.write("\n".join(dessert))
with open("./listpack/drinks.txt","w") as f: f.write("\n".join(drink))
with open("./listpack/seafood.txt","w") as f: f.write("\n".join(seafood))
with open("./listpack/appetizers.txt","w") as f: f.write("\n".join(appetizer))
with open("./listpack/side_dishes.txt","w") as f: f.write("\n".join(side_dish))
